<template>
	<view class="container">
			<uni-forms  :modelValue="formData">
				<uni-forms-item label="货号" name="item_bn">
					<uni-easyinput type="text" v-model="formData.item_bn" placeholder="*请输入货号" />
				</uni-forms-item> 
				<uni-forms-item label="商品名称" name="name">
					<uni-easyinput type="text" v-model="formData.name" placeholder="*请输入商品名称" />
				</uni-forms-item>  
				<uni-forms-item label="分类" name="itemscategory_id">
					<uni-data-select
					  v-model="formData.itemscategory_id"
					  :localdata="categoryData"
					  @change="change"
					></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="所属位置" name="position">
					<uni-data-select
					  v-model="formData.position"
					  :localdata="positionData"
					  @change="changePosition"
					></uni-data-select>
				</uni-forms-item> 
				<uni-forms-item label="位置信息" name="position2">
					<uni-data-select
					  v-model="formData.position2"
					  :localdata="position2Data"
					  @change="changePosition2"
					></uni-data-select>
				</uni-forms-item> 
				<uni-forms-item label="位置备注" name="position3">
					<uni-easyinput type="text" v-model="formData.position3" placeholder="请输入位置备注(选填)" />
				</uni-forms-item> 
				
				    
			</uni-forms>
			<button @click="submitForm" type="primary">保存</button>  
	
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData:{itemscategory_id:'',position:'',position2:''}, 
				categoryData:[],
				positionData:[],
				position2Data:[],
				id:'',
				domain:"http://"+localStorage.getItem('config_ip')+":"+localStorage.getItem('config_port'),
			}
		},
		methods: {
			change(e) {
			        console.log("e:", e);
			      },
				  changePosition(e){
					   console.log("e:", e);
				  },
				  changePosition2(e){
				  		console.log("e:", e);
				  },
			submitForm(){
				if(!localStorage.getItem('config_ip') ){
					uni.showToast({
						duration:2000,
						icon:'error',
						title:'地址配置不能为空！'
					})
					uni.navigateTo({
						url:'/pages/config/config'
					})
					return false;
				}else{
					if(this.searchValue == ''){
						uni.showToast({
							duration:2000,
							icon:'error',
							title:'货号不能为空！'
						})
						return false;
					}else{
						//搜索
						uni.request({
							url:this.domain+'/api/items/edit',
							data:this.formData,
							method:'POST',
							success(res) {
								uni.showToast({
									duration:2000,
									icon:'success',
									title:'编辑成功！'
								})
							},fail(err) {
								uni.showToast({
									duration:2000,
									icon:'error',
									title:'编辑失败'
								})
							}
						})
					}
				}
			}, 
			cancel(){
				
			},
			clear(){
				
			},
			getCategoryList(){
				let that =this;
				uni.request({
					url:this.domain+'/api/items/getCategory',
					success(res) {
						console.log(res.data.data.list);
						that.categoryData = res.data.data.list
					}
				})
			},
			getPosition(){
				let that =this;
				uni.request({
					url:this.domain+'/api/items/getPosition',
					success(res) {
						that.positionData = res.data.data.list
					}
				})
			},
			getPosition2(){
				let that =this;
				uni.request({
					url:this.domain+'/api/items/getPosition2',
					success(res) {
						that.position2Data = res.data.data.list
					}
				})
			},
			getInfo(searchValue){
				let that = this
				uni.request({
					url:that.domain+'/api/items/getInfo?item_bn='+searchValue,
					success(res) {
						if(res.data.data.list){
							that.formData = res.data.data.list[0] 
						}
					}
				})
			},
		},
		onLoad(options){
			this.getCategoryList();
			this.getPosition();
			this.getPosition2();
			if(this.options.item_bn != undefined){
				this.getInfo(this.options.item_bn);
			}
		}
	}
</script>

<style>
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
	}
</style>
